package com.chenggong.log

import android.util.Log
import androidx.appcompat.app.AppCompatActivity

/**
 * Created by chenggong on 18-5-2023.
 */
object LogDebugUtil {

    private var viewLog: ViewLog? = null
    private const val TAG = "LogDebugUtil"
    private var isAllowPrint = true//默认允许打印日志

    fun instructions(): String{
        return  "LogDebugUtil.init(true)//允许打印日志\n" +
                "LogDebugUtil.init(false)//禁止打印日志\n\n" +
                "LogDebugUtil.d(tag, message)\n" +
                "LogDebugUtil.d(message)\n" +
                "LogDebugUtil.e(tag, message)\n" +
                "LogDebugUtil.e(message)\n" +
                "LogDebugUtil.i(tag, message)\n" +
                "LogDebugUtil.i(message)\n" +
                "LogDebugUtil.v(tag, message)\n" +
                "LogDebugUtil.v(message)\n" +
                "LogDebugUtil.w(tag, message)\n" +
                "LogDebugUtil.w(message)\n" +
                "LogDebugUtil.wtf(tag, message)\n" +
                "LogDebugUtil.wtf(message)\n" +
                "LogDebugUtil.println(message)\n" +
                "LogDebugUtil.createViewLog(activity: AppCompatActivity)\n" +
                "LogDebugUtil.showViewLog()\n" +
                "视图日志（ViewLog），使手机端也可以查看到日志信息"
    }

    private fun makeMessage(message: Any?): String {
        message ?: return ""
        return when (message) {
            is String -> {
                message
            }
            is Throwable -> {
                message.message.toString()
            }
            is Int, Long, Float, Double, Boolean, Char, Short, Byte -> {
                message.toString()
            }
            is Array<*> -> {
                message.contentToString()
            }
            else -> message.toString()
        }
    }

    fun createViewLog(activity: AppCompatActivity){
        viewLog = ViewLog(activity)
    }

    fun showViewLog() {
        if(viewLog == null) {
            e("You first need to use functions \"createViewLog(activity: AppCompatActivity)\" to create a view log")
        }else{
            viewLog?.show()
        }
    }

    fun init(isAllowPrint: Boolean) {
        this.isAllowPrint = isAllowPrint
    }

    fun println(message: String) {
        if (isAllowPrint) {
            kotlin.io.println("From LogDebugUtil:$message")
        }
    }

    fun d(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.d(tag, msg)
            viewLog?.d(tag, msg)
        }
    }

    fun d(message: Any?) {
        d(TAG, message)
    }

    fun e(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.e(tag, msg)
            viewLog?.e(tag, msg)
        }
    }

    fun e(message: Any?) {
        e(TAG, message)
    }

    fun i(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.i(tag, msg)
            viewLog?.i(tag, msg)
        }
    }

    fun i(message: Any?) {
        i(TAG, message)
    }

    fun v(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.v(tag, msg)
            viewLog?.v(tag, msg)
        }
    }

    fun v(message: Any?) {
        v(TAG, message)
    }

    fun w(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.w(tag, msg)
            viewLog?.w(tag, msg)
        }
    }

    fun w(message: Any?) {
        w(TAG, message)
    }

    fun wtf(tag: String, message: Any?) {
        if (isAllowPrint) {
            val msg = makeMessage(message)
            Log.wtf(tag, msg)
            viewLog?.wtf(tag, msg)
        }
    }

    fun wtf(message: Any?) {
        wtf(TAG, makeMessage(message))
    }

}